package computer;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.BevelBorder;

public class Computer extends JFrame implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField tf;
	private boolean isNew=true;
	private boolean islx=false;
	private double s=0;
	private String preop="+";
	
	public Computer() {
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(500, 300);
		setLocation(400, 200);
		
		Font fon=new Font("微软雅黑", Font.TRUETYPE_FONT, 24);
		
		JPanel top=new JPanel();
		top.setLayout(new BorderLayout());
		tf=new JTextField("0");
		tf.setFont(fon);
		tf.setHorizontalAlignment(JTextField.RIGHT);
		tf.setEnabled(false);
		tf.setDisabledTextColor(Color.BLUE);
		JButton cls=new JButton("清除");
		cls.addActionListener(this);
		cls.setBackground(new Color(0xDEB887));
		cls.setFont(fon);
		top.add(tf);  top.add(cls,BorderLayout.EAST);
		
		JPanel center=new JPanel();
		center.setLayout(new GridLayout(4, 4));
		String[] str={"7","8","9","+","4","5","6","-","1","2","3","*","0",".","=","/"};
		for(String s:str){
			JButton bt=new JButton(s);
			bt.setFont(fon);
			bt.setBackground(new Color(0xDEB887));
			bt.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
			bt.addActionListener(this);
			center.add(bt);
		}
		
		
		add(top,BorderLayout.NORTH);
		add(center);
	}
	
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			
			@Override
			public void run() {
				new Computer();
			}
		});
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String cmd=e.getActionCommand();
		if("0.123456789".indexOf(cmd)>=0){
			showNUm(cmd);
		}
		else if(cmd.equals("清除")){
			tf.setText("0");
			s=0;
			preop="+";
			isNew=true;
			islx=false;
		}
		else{
			compute(cmd);
		}
	}

	private void compute(String op) {
		isNew=true;
		if(islx){preop=op; return;}
		double cur=Double.valueOf(tf.getText());
		switch(preop){
		case "+":s+=cur;break;
		case "-":s-=cur;break;
		case "*":s*=cur;break;
		case "/":s/=cur;break;
		}
		preop=op;
		String str=String.valueOf(s);
		if(str.endsWith(".0")) str=str.substring(0, str.length()-2);
		if(str.equals("Infinity")) str="除数不能为0";
		tf.setText(str);
		islx=true;
	}

	private void showNUm(String cmd) {
		islx=false;
		String yuan=tf.getText();
		if(isNew==true) {yuan="0";isNew=false;}
		if(yuan.indexOf(".")>=0&&cmd.equals(".")) return;
		if(yuan.equals("0")){
			if(cmd.equals(".")) tf.setText("0.");
			else tf.setText(cmd);
		}
		else{
			tf.setText(yuan+cmd);
		}
	}

}
